package com.zeroext.show.service.impl;

import java.util.List;
import java.util.Set;

import com.zeroext.service.common.BaseService;
import com.zeroext.show.dao.TopoDAO;
import com.zeroext.show.entity.NetworkTopo;
import com.zeroext.show.entity.TopoElement;
import com.zeroext.show.service.TopoService;

public class TopoServiceImpl extends BaseService implements TopoService {
    private TopoDAO topoDAO;
    
    public NetworkTopo createNetworkTopo(String title, String bg) {
        NetworkTopo topo = new NetworkTopo();
        topo.setTitle(title);
        topo.setBg(bg);
        return topoDAO.saveNetworkTopo(topo);
    }

    public void deleteNetworkTopo(Long id) {
        topoDAO.deleteNetworkTopo(id);
    }

    public NetworkTopo saveNetworkTopo(NetworkTopo topo, Set<Long> deletes,
            Set<TopoElement> adds, Set<TopoElement> modifys) {
        NetworkTopo pTopo = topoDAO.getNetworkTopo(topo.getId());
        pTopo.setTitle(topo.getTitle());
        pTopo.setBg(topo.getBg());
        Set<TopoElement> els = pTopo.getElements();
        
        // delete
        if(deletes!=null){
            for(Long deleteElId : deletes){
                TopoElement deleteEl = new TopoElement();
                deleteEl.setId(deleteElId);
                try{
                    els.remove(deleteEl);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
        
        if(adds!=null){
            for(TopoElement add : adds){
                topoDAO.saveTopoElement(add);
                els.add(add);
            }
        }
        
        if(modifys!=null){
            for(TopoElement modify : modifys){
                els.add(modify);
            }
        }
        return topoDAO.saveNetworkTopo(topo);
    }

    public NetworkTopo getNetworkTopo(Long id) {
        return topoDAO.getNetworkTopo(id);
    }

    public List<NetworkTopo> listNetworkTopo() {
        return topoDAO.searchNetworkTopo();
    }
    
    public TopoDAO getTopoDAO() {
        return topoDAO;
    }

    public void setTopoDAO(TopoDAO topoDAO) {
        this.topoDAO = topoDAO;
    }

}
